/**
 * login模块接口列表
 */

import base from '@/common/base'; // 导入接口域名列表
import axios from '@/common/http'; // 导入http中创建的axios实例
import qs from 'qs';
import router from "../router"; // 根据需求是否导入qs模块
import {Dialog} from 'vant';
import storeJs from 'storejs'

const login = {
  // 注册
  register (params) {
    return axios({
      method: 'post',
      url: `${base.sq}Account/register`,
      data:qs.stringify(params),
      headers:{'user':1}
    });
  },

  // 登陆
  login(params) {
    return axios({
      method: 'post',
      url: `${base.sq}Account/login`,
      data:qs.stringify(params),
      headers:{'user':1}
    });
  },

  //获取验证码
  getCode(params){
    return axios({
      method: 'post',
      url: `${base.sq}Account/sendVerify`,
      data:qs.stringify(params),
      headers:{'user':1}
    });
  },

    checkLogin(callBackFun,status=true){
        let check=false;
        axios({
            method: 'get',
            url: `${base.sq}My/index`,
            headers:{'checkLogin':1,'load':1}
        }).then(res=>{
            callBackFun(true);
        }).catch(res=>{

            if(status){
                if(res.status==401){
                    Dialog.confirm({
                        message: "您还没登录，您确定马上去登陆吗？"
                    }).then(() => {
                        router.replace({
                            path: '/login',
                            query: {
                                redirect: router.currentRoute.fullPath
                            }
                        });
                    });
                }else if (res.status=403){
                    storeJs.remove("carToken");
                    storeJs('loginSuccess', null);

                    Dialog.confirm({
                        message: "您还没登录，您确定马上去登陆吗？"
                    }).then(() => {
                        router.replace({
                            path: '/login',
                            query: {
                                redirect: router.currentRoute.fullPath
                            }
                        });
                    });
                }
            }

        });
    },
}

export default login;
